package com.xydtech.workflow.service.impl;

import com.xydtech.workflow.domain.WflApprauthor;
import com.xydtech.workflow.domain.WflFlowstep;
import com.xydtech.workflow.mapper.WflApprauthorMapper;
import com.xydtech.workflow.mapper.WflFlowstepMapper;
import com.xydtech.workflow.service.IWflApprauthorService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * 流程审批权限列Service业务层处理
 *
 * @author JHON
 * @date 2021-05-27
 */
@Service("apprauthor")
public class WflApprauthorServiceImpl implements IWflApprauthorService {
    @Autowired
    private WflApprauthorMapper wflApprauthorMapper;
    @Autowired
    private WflFlowstepMapper wflFlowstepMapper;

    /**
     * 查询流程审批权限列列表
     *
     * @param versionId 流程版本号
     * @param stepId 流程节点号
     * @return 流程审批权限列集合
     */
    @Override
    public List<WflApprauthor> selectWflApprauthorList(Long versionId, Long stepId) {
        return wflApprauthorMapper.selectWflApprauthorList(versionId, stepId);
    }

    /**
     * 查询流程步骤审批权限
     *
     * @param versionId 流程版本号
     * @param stepId 流程节点号
     * @return 流程审批权限列集合
     */
    @Override
    public List<WflApprauthor> selectStepApprAuthorList(Long versionId, Long stepId) {
        return wflApprauthorMapper.selectStepApprAuthorList(versionId, stepId);
    }

    /**
     *新增流程审批权限列
     * @Title: insertWflApprauthor
     * @param versionId 流程版本号
     * @return int
     * @exception
     * @author JHON
     * @Date 2021/5/27 16:02
     **/
    @Override
    public int insertWflApprauthor(Long versionId) {
        //先查询流程节点
        List<WflFlowstep> stepList = wflFlowstepMapper.selectWflFlowstepById(versionId);
        //定义审批权限列表
        List<WflApprauthor> approveList = new ArrayList<WflApprauthor>();
        for (int i = 0; i < stepList.size(); i++) {
            // 每个节点都有同意和拒件权限
            //加工同意权限信息
            WflApprauthor agreeWflApprauthor = new WflApprauthor();
            agreeWflApprauthor.setVersionId(versionId);
            agreeWflApprauthor.setApprovCode("T");
            agreeWflApprauthor.setStepId(stepList.get(i).getStepId());
            agreeWflApprauthor.setApprovDesc("同意");
            agreeWflApprauthor.setOrderNum(1L);
            approveList.add(agreeWflApprauthor);
            //加工拒件权限信息
            WflApprauthor refuseWflApprauthor = new WflApprauthor();
            refuseWflApprauthor.setVersionId(versionId);
            refuseWflApprauthor.setApprovCode("F");
            refuseWflApprauthor.setStepId(stepList.get(i).getStepId());
            refuseWflApprauthor.setApprovDesc("拒件");
            refuseWflApprauthor.setOrderNum(2L);
            approveList.add(refuseWflApprauthor);
            //查询是否有可退回步骤
            List<WflFlowstep> backStepList = selectApprBackStep(versionId, stepList.get(i).getOrderNum());
            // 第二步开始有退回之前节点步骤权限
            if (backStepList.size() > 0) {
                for (int j = 0; j < backStepList.size(); j++) {
                    //加工退回权限信息
                    WflApprauthor returnWflApprauthor = new WflApprauthor();
                    returnWflApprauthor.setVersionId(versionId);
                    returnWflApprauthor.setApprovCode("R");
                    returnWflApprauthor.setStepId(stepList.get(i).getStepId());
                    returnWflApprauthor.setApprovDesc("退回到" + backStepList.get(j).getStepName());
                    returnWflApprauthor.setReturnStepId(backStepList.get(j).getStepId());
                    returnWflApprauthor.setOrderNum((long) (3 + j));
                    approveList.add(returnWflApprauthor);
                }
            }
        }
        //先删除原有数据
        deleteWflApprauthorById(versionId);
        //插入权限列表数据
        return wflApprauthorMapper.insertWflApprauthor(approveList);
    }

    /**
     * 查询可以退回的流程节点步骤
     * @Title: selectApprBackStep
     * @param versionId
     * @param orderNum
     * @return java.util.List<com.xydtech.workflow.domain.WflFlowstep>
     * @exception
     * @author JHON
     * @Date 2021/5/27 15:54
     **/
    @Override
    public List<WflFlowstep> selectApprBackStep(Long versionId, Long orderNum) {
        WflFlowstep wflFlowstep = new WflFlowstep();
        wflFlowstep.setVersionId(versionId);
        wflFlowstep.setOrderNum(orderNum);
        return wflFlowstepMapper.selectApprBackStep(wflFlowstep);
    }


    /**
     * 删除流程审批权限列信息
     *
     * @param versionId 流程审批权限列ID
     * @return 结果
     */
    @Override
    public int deleteWflApprauthorById(Long versionId) {
        return wflApprauthorMapper.deleteWflApprauthorById(versionId);
    }
}
